Java Technologies Generic Methods এবং Exception Propagation গাইড ও নোট

348

Generic Methods জেনেরিক্স ব্যবহার করে এমন মেথড যা নির্দিষ্ট টাইপের উপর নির্ভর না করে কাজ করতে পারে। অন্যদিকে, Exception Propagation হল প্রোগ্রামে ত্রুটি (Exception) ঘটলে কীভাবে তা এক লেয়ার থেকে অন্য লেয়ারে পাঠানো হয়।


Generic Methods

Generic Method এমন একটি মেথড যেখানে মেথডের সিগনেচারে জেনেরিক টাইপ প্যারামিটার ডিফাইন করা হয়। এই প্যারামিটার টাইপ মেথড কল করার সময় নির্ধারিত হয়।

Generic Method এর কাঠামো:

public <T> ReturnType methodName(T parameter) {
    // method body
}

উদাহরণ: Generic Method ব্যবহার

public class GenericExample {
    // A generic method that prints any type of array
    public static <T> void printArray(T[] array) {
        for (T element : array) {
            System.out.print(element + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3, 4, 5};
        String[] strArray = {"Java", "Generics", "Method"};

        printArray(intArray); // Output: 1 2 3 4 5
        printArray(strArray); // Output: Java Generics Method
    }
}

Key Points:

  1. printArray মেথডটি যেকোনো টাইপের অ্যারে প্রিন্ট করতে পারে।
  2. <T> হল জেনেরিক টাইপ প্যারামিটার।

Bounded Type Parameters সহ Generic Methods

মাঝে মাঝে মেথডে ব্যবহৃত টাইপ প্যারামিটারকে সীমাবদ্ধ করা প্রয়োজন। এ ক্ষেত্রে, bounded type parameters ব্যবহার করা হয়।

public class BoundedExample {
    // A generic method that calculates the sum of numbers
    public static <T extends Number> double sumArray(T[] array) {
        double sum = 0.0;
        for (T element : array) {
            sum += element.doubleValue();
        }
        return sum;
    }

    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3, 4, 5};
        Double[] doubleArray = {1.1, 2.2, 3.3};

        System.out.println("Sum of Integer array: " + sumArray(intArray)); // Output: 15.0
        System.out.println("Sum of Double array: " + sumArray(doubleArray)); // Output: 6.6
    }
}

ব্যাখ্যা:

  • টাইপ প্যারামিটার <T extends Number> দিয়ে নিশ্চিত করা হয় যে, শুধুমাত্র Number ক্লাসের সাবটাইপ ব্যবহার করা যাবে।

Generic Methods এবং Exception Propagation

Generic Method থেকে Checked Exception Propagation

Generic Methods এর সাথে Exception Propagation করতে হলে Exception Type কে জেনেরিক টাইপ হিসেবে ডিফাইন করা যেতে পারে।

import java.util.concurrent.Callable;

public class ExceptionPropagationExample {
    // A generic method with exception propagation
    public static <T> T execute(Callable<T> task) throws Exception {
        return task.call();
    }

    public static void main(String[] args) {
        try {
            // Example of a successful execution
            String result = execute(() -> "Hello, Generics!");
            System.out.println(result);

            // Example of a failing execution
            execute(() -> {
                throw new Exception("Something went wrong!");
            });
        } catch (Exception e) {
            System.err.println("Caught Exception: " + e.getMessage());
        }
    }
}

ব্যাখ্যা:

  1. execute মেথডটি Callable<T> ইন্টারফেস ব্যবহার করে জেনেরিক এক্সিকিউশন পরিচালনা করে।
  2. throws Exception ব্যবহার করে Checked Exception এর Propagation নিশ্চিত করা হয়।

Generic Methods এবং Custom Exceptions

Custom Exceptions এর মাধ্যমে জেনেরিক মেথডে নির্দিষ্ট ধরনের ত্রুটি পরিচালনা করা যায়।

class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class CustomExceptionExample {
    public static <T> void validateNotNull(T value) throws CustomException {
        if (value == null) {
            throw new CustomException("Value cannot be null!");
        }
        System.out.println("Validated: " + value);
    }

    public static void main(String[] args) {
        try {
            validateNotNull("Java Generics"); // Valid input
            validateNotNull(null); // Will throw CustomException
        } catch (CustomException e) {
            System.err.println("Caught Exception: " + e.getMessage());
        }
    }
}

Key Features:

  1. Custom Exception ব্যবহারের মাধ্যমে স্পষ্ট ত্রুটি পরিচালনা।
  2. জেনেরিক টাইপের জন্য ভ্যালিডেশন যুক্ত করা।

  1. Generic Methods কোডের পুনরায় ব্যবহারযোগ্যতা এবং টাইপ সেফটি বাড়ায়।
  2. Exception Propagation এর মাধ্যমে জেনেরিক মেথডে ত্রুটি পরিচালনা সহজ হয়।
  3. Custom Exceptions এবং Bounded Type Parameters জেনেরিক মেথডে আরও কার্যকর কাস্টমাইজেশন এবং টাইপ কন্ট্রোল প্রদান করে।
  4. প্রজেক্টের জটিলতার উপর নির্ভর করে Generic Methods এবং Exception Propagation ব্যবহার করা যায়।
Content added By
Promotion

Are you sure to start over?

Loading...